/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.theme.passport.controller.app;

import com.theme.common.base.StringHandler;
import com.theme.common.mvc.exception.RRException;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.oss.cloud.OSSFactory;
import com.theme.passport.entity.UploadFileEntity;
import com.theme.passport.service.UploadFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @name: AppUploadController <tb>
 * @title: APP上传文件  <tb>
 * @author: cuixinfu@51play.com <tb>
 * @date: 2021/12/28 11:19:12<tb>
 */
@RestController
@RequestMapping("/app/upload")
@Api("APP上传文件接口")
public class AppUploadController {

    @Autowired
    private UploadFileService uploadFileService;

    @PostMapping("file")
    @ApiOperation("上传文件")
    public R order(@RequestParam("file") MultipartFile file) throws IOException {

        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        // 上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build(null).uploadSuffix(file.getBytes() ,suffix);

        // 保存文件信息
        UploadFileEntity uploadFile = new UploadFileEntity();
        uploadFile.setUrl(url);
        uploadFile.setName(file.getName());
        uploadFile.setSize(file.getSize());
        uploadFile.setCreateTime(new Date());
        uploadFileService.save(uploadFile);

        return R.ok().put("url", url);
    }

    @Login
    @PostMapping("deleteObject")
    @ApiOperation("删除单个文件")
    public R deleteObject(@RequestParam("file") String firstKey) {

        if (StringHandler.isEmpty(firstKey)) {
            throw new RRException("删除文件路径不能为空");
        }
        boolean flag = OSSFactory.build(null).deleteObject(firstKey);

        return R.ok().put("success", flag);
    }

    @Login
    @PostMapping("deleteObjectKeys")
    @ApiOperation("删除多个文件")
    public R deleteObjectKeys(@RequestParam("files") List<String> files) {

        boolean flag = OSSFactory.build(null).deleteObjectKeys(files);

        return R.ok().put("success", flag);
    }

    @Login
    @PostMapping("deleteObjectDir")
    @ApiOperation("删除目录以及目录下文件")
    public R deleteObjectDir(@RequestParam("dirPrefix") String dirPrefix) {

        boolean flag = OSSFactory.build(null).deleteObjectDir(dirPrefix);

        return R.ok().put("success", flag);
    }

}
